home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl1 / examples / lighting / materials.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  7.0 KB  |  286 lines

  1. /* Copyright 1993, 1996 Silicon Graphics, Inc.
  2.  * All Rights Reserved.
  3.  *
  4.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  5.  * the contents of this file may not be disclosed to third parties, copied or
  6.  * duplicated in any form, in whole or in part, without the prior written
  7.  * permission of Silicon Graphics, Inc.
  8.  *
  9.  * RESTRICTED RIGHTS LEGEND:
  10.  * Use, duplication or disclosure by the Government is subject to restrictions
  11.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  12.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  13.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  14.  * rights reserved under the Copyright Laws of the United States.
  15.  */
  16.  
  17. /*  materials.c - Use material properties to change the color and
  18.  *        reflection models for some objects
  19.  *
  20.  *  Left Mouse Button        - change incidence and azimuth angles
  21.  *  Middle Mousebutton        - change the twist angle based on
  22.  *                  horizontal mouse movement
  23.  *  Right Mousebutton        - zoom in and out based on vertical
  24.  *                  mouse movement
  25.  *  <R> Key            - reset viewpoint
  26.  *  Escape key            - exit the program
  27.  */
  28. #include <GL/gl.h>
  29. #include <GL/glu.h>
  30. #include <GL/glut.h>
  31.  
  32. #include <math.h>
  33. #include <stdio.h>
  34.  
  35. /*  Function Prototypes  */
  36.  
  37. GLvoid  initgfx( GLvoid );
  38. GLvoid  drawScene( GLvoid );
  39. GLvoid  reshape( GLsizei, GLsizei );
  40. GLvoid  keyboard( GLubyte, GLint, GLint );
  41. GLvoid  mouse( GLint, GLint, GLint, GLint );
  42. GLvoid  motion( GLint, GLint );
  43.  
  44. void resetView( GLvoid );
  45. void polarView( GLfloat, GLfloat, GLfloat, GLfloat );
  46. void printHelp( char * );
  47.  
  48. /* Global Definitions */
  49.  
  50. #define KEY_ESC    27    /* ascii value for the escape key */
  51.  
  52. /* Global Variables */
  53.  
  54. static enum        actions { MOVE_EYE, TWIST_EYE, ZOOM, MOVE_NONE };
  55. static GLint        action;
  56.  
  57. static GLdouble        xStart = 0.0, yStart = 0.0;
  58.  
  59. static GLfloat         fovy, near, far, distance, twistAngle, incAngle, azimAngle;
  60.  
  61. void
  62. main( int argc, char *argv[] )
  63. {
  64.     GLsizei width, height;
  65.  
  66.     glutInit( &argc, argv );
  67.  
  68.     width = glutGet( GLUT_SCREEN_WIDTH ); 
  69.     height = glutGet( GLUT_SCREEN_HEIGHT );
  70.     glutInitWindowPosition( width / 4, height / 4 );
  71.     glutInitWindowSize( (width / 2) - 4, height / 2 );
  72.     glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE );
  73.     glutCreateWindow( argv[0] );
  74.  
  75.     initgfx();
  76.  
  77.     glutMouseFunc( mouse );
  78.     glutMotionFunc( motion );
  79.     glutKeyboardFunc( keyboard );
  80.     glutReshapeFunc( reshape );
  81.     glutDisplayFunc( drawScene ); 
  82.  
  83.     printHelp( argv[0] );
  84.  
  85.     glutMainLoop();
  86. }
  87.  
  88. void
  89. printHelp( char *progname )
  90. {
  91.     fprintf(stdout, 
  92.         "\n%s - demonstrate lighting material properties\n\n" 
  93.         "Left Mousebutton    - move eye position\n"
  94.         "Middle Mousebutton    - change twist angle\n"
  95.         "Right Mousebutton    - move up / down to zoom in / out\n"
  96.         "<R> Key        - reset viewpoint\n"
  97.         "Escape Key        - exit the program\n\n",
  98.         progname);
  99. }
  100.  
  101. GLvoid
  102. initgfx( GLvoid )
  103. {
  104.     glClearColor( 0.0, 0.0, 0.0, 1.0 );
  105.  
  106.     glEnable( GL_DEPTH_TEST );
  107.  
  108.     fovy = 60.0;    /* field of view in Y */
  109.     near = 3.0;    /* Near clipping plane location */
  110.     far  = 12.0;    /* Far clipping plane location */
  111.  
  112.     resetView();
  113.  
  114.     /* Turn on a default light */
  115.     glEnable( GL_LIGHT0 );
  116. }
  117.  
  118. GLvoid 
  119. keyboard( GLubyte key, GLint x, GLint y )
  120. {
  121.     switch (key) {
  122.     case 'R':
  123.         resetView();
  124.         glutPostRedisplay();
  125.         break;
  126.     case KEY_ESC:    /* Exit whenever the Escape key is pressed */
  127.         exit(0);
  128.     }
  129. }
  130.  
  131. GLvoid 
  132. mouse( GLint button, GLint state, GLint x, GLint y )
  133. {
  134.     static GLint buttons_down = 0;
  135.  
  136.     if (state == GLUT_DOWN) {
  137.         switch (button) {
  138.         case GLUT_LEFT_BUTTON:
  139.             action = MOVE_EYE;
  140.             break;
  141.         case GLUT_MIDDLE_BUTTON:
  142.             action = TWIST_EYE;
  143.             break;
  144.         case GLUT_RIGHT_BUTTON:
  145.             action = ZOOM;
  146.             break;
  147.         }
  148.  
  149.         /* Update the saved mouse position */
  150.         xStart = x;
  151.         yStart = y;
  152.     } else {
  153.         if (--buttons_down == 0) 
  154.             action = MOVE_NONE;
  155.     }
  156.  
  157. }
  158.  
  159. GLvoid
  160. motion( GLint x, GLint y )
  161. {
  162.     switch (action) {
  163.     case MOVE_EYE:
  164.         /* Adjust the eye position based on the mouse position */
  165.         azimAngle += (GLdouble) (x - xStart);
  166.         incAngle -= (GLdouble) (y - yStart);
  167.         break;
  168.     case TWIST_EYE:
  169.         /* Adjust the eye twist based on the mouse position */
  170.         twistAngle = fmodf(twistAngle+(x - xStart), 360.0);
  171.         break;
  172.     case ZOOM:
  173.         /* Adjust the eye distance based on the mouse position */
  174.         distance -= (GLdouble) (y - yStart)/10.0;
  175.         break;
  176.     default:
  177.         printf("unknown action %d\n", action);
  178.     }
  179.     
  180.     /* Update the stored mouse position for later use */
  181.     xStart = x;
  182.     yStart = y;
  183.  
  184.     glutPostRedisplay();
  185. }
  186.  
  187. void
  188. resetView( GLvoid )
  189. {
  190.     distance = near + (far - near) / 2.0;
  191.     twistAngle = 0.0;    /* rotation of viewing volume (camera) */
  192.     incAngle = 60.0;
  193.     azimAngle = 0.0;
  194.     fovy = 60.0;    /* Field of view in Y angle */
  195. }
  196.  
  197. GLvoid
  198. reshape( GLsizei width, GLsizei height )
  199. {
  200.     GLdouble    aspect;
  201.  
  202.     glViewport( 0, 0, width, height );
  203.  
  204.     aspect = (GLdouble) width / (GLdouble) height;
  205.  
  206.     glMatrixMode( GL_PROJECTION );
  207.     glLoadIdentity();
  208.     gluPerspective( fovy, aspect, near, far );
  209.     glMatrixMode( GL_MODELVIEW );
  210. }
  211.  
  212. void
  213. polarView( GLfloat distance, GLfloat azimuth, GLfloat incidence,
  214.             GLfloat twist)
  215. {
  216.     glTranslatef( 0.0, 0.0, -distance);
  217.     glRotatef( -twist, 0.0, 0.0, 1.0);
  218.     glRotatef( -incidence, 1.0, 0.0, 0.0);
  219.     glRotatef( -azimuth, 0.0, 0.0, 1.0);
  220. }
  221.  
  222. GLvoid
  223. drawScene( GLvoid )
  224. {
  225.     /* Define a few materials properties */
  226.     GLfloat   redAmbient[] = { 0.3, 0.1, 0.1, 1.0 };
  227.     GLfloat   redDiffuse[] = { 1.0, 0.0, 0.0, 1.0 };
  228.     GLfloat   blueAmbient[] = { 0.1, 0.1, 0.3, 1.0 };
  229.     GLfloat   blueDiffuse[] = { 0.0, 0.0, 1.0, 1.0 };
  230.     GLfloat   yellowDiffuse[] = { 1.0, 1.0, 0.0, 1.0 };
  231.     GLfloat   yellowEmission[] = { 0.6, 0.6, 0.0, 1.0 };
  232.     GLfloat   defaultEmission[] = { 0.0, 0.0, 0.0, 1.0 };
  233.     GLfloat   whiteSpecular[] = { 1.0, 1.0, 1.0, 1.0 };
  234.     GLfloat   greenSpecular[] = { 0.0, 1.0, 0.0, 1.0 };
  235.     GLfloat   defaultSpecular[] = { 0.0, 0.0, 0.0, 1.0 };
  236.  
  237.     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  238.  
  239.     glPushMatrix();
  240.         polarView( distance, azimAngle, incAngle, twistAngle );
  241.  
  242.         XYZaxes();
  243.  
  244.         glEnable( GL_LIGHTING );
  245.  
  246.         glMaterialfv( GL_FRONT, GL_EMISSION, defaultEmission );
  247.  
  248.         /* Set properties for a shiny red material,
  249.          * with a green highlight */
  250.         glMaterialfv( GL_FRONT, GL_AMBIENT, redAmbient );
  251.         glMaterialfv( GL_FRONT, GL_DIFFUSE, redDiffuse );
  252.         glMaterialfv( GL_FRONT, GL_SPECULAR, greenSpecular );
  253.         glMaterialf( GL_FRONT, GL_SHININESS, 128.0 );
  254.         glPushMatrix();
  255.             glTranslatef( -2.0, 1.5, 0.0 );
  256.             glutSolidSphere( 0.7, 31, 31 );
  257.         glPopMatrix();
  258.  
  259.         /* Set properties for a dull blue material with
  260.          *   a small white highlight */
  261.         glMaterialfv( GL_FRONT, GL_AMBIENT, blueAmbient );
  262.         glMaterialfv( GL_FRONT, GL_DIFFUSE, blueDiffuse );
  263.         glMaterialfv( GL_FRONT, GL_SPECULAR, whiteSpecular );
  264.         glMaterialf( GL_FRONT, GL_SHININESS, 20.0 );
  265.         glPushMatrix();
  266.             glTranslatef( 2.5, 0.0, 0.0 );
  267.             glutSolidTorus( 0.25, 0.75, 16, 31 );
  268.         glPopMatrix();
  269.  
  270.         /* Set properties for a yellow glowing material */
  271.         glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, yellowDiffuse);
  272.         glMaterialfv( GL_FRONT, GL_EMISSION, yellowEmission );
  273.         glMaterialfv( GL_FRONT, GL_SPECULAR, defaultSpecular );
  274.  
  275.         glPushMatrix();
  276.             glTranslatef( 0.0, 2.0, 2.0 );
  277.             glutSolidCube( 0.5 );
  278.         glPopMatrix();
  279.  
  280.         glDisable( GL_LIGHTING );
  281.  
  282.     glPopMatrix();
  283.  
  284.     glutSwapBuffers();
  285. }
  286.